package com.example.onlinemonitoring.serviceImpl;

import com.example.onlinemonitoring.model.Gwblqsensor;
import com.example.onlinemonitoring.model.Appdevice;
import com.example.onlinemonitoring.model.Gwblqhistory;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.example.onlinemonitoring.Util.tem.SensorAbstractBaseService;
import com.example.onlinemonitoring.service.GwblqsensorService;
import com.example.onlinemonitoring.model.common.chartres;
import com.example.onlinemonitoring.Util.HexUtil;
import com.example.onlinemonitoring.dao.GwblqhistoryMapper;
import com.example.onlinemonitoring.dao.GwblqsensorMapper;
import tk.mybatis.mapper.entity.Example;
import com.example.onlinemonitoring.model.common.MyResult;
import com.example.onlinemonitoring.model.common.ptconfig;
import com.example.onlinemonitoring.model.common.wsdconfig;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
* @description Gwblqsensor接口实现
* @author  generator
* @since  2022-10-15 15:00:16
*/
@Service("gwblqServiceImpl")
public class GwblqsensorServiceImpl extends SensorAbstractBaseService<Gwblqsensor, Gwblqhistory> implements GwblqsensorService {
     //这里需要重写getDt方法,用来处理图表数据
    //public String[] getDt(List<Gwblqhistory> list,String[] dt,chartres cts,float[][] sres,SimpleDateFormat sdf,SimpleDateFormat csdf,SimpleDateFormat sdf2){return null;}
    @Autowired
    private GwblqsensorMapper gwblqsensormapper;
    @Autowired
    private GwblqhistoryMapper gwblqhistorymapper;
    public void UpdateStatus(Appdevice ad, Integer addrindex, int i, String string) {
        Gwblqsensor record = new Gwblqsensor();
        record.setStationid(ad.getStationid());
        record.setDevicegroup(ad.getDevicegroup());
        switch (string) {
            case "blq":
            record.setLastcommunicationstatusBlq(i);
                break;
            case "pt":
            record.setLastcommunicationstatusPt(i);
                break;
            case "wsd":
            record.setLastcommunicationstatusWsd(i);
                break;
            default:
                break;
        }
        if(i != 0){
            record.setLastalarmlevel(-1);
        }
        record.setDeviceaddr(addrindex);
        Update(record, false);
    }
    public List<Gwblqhistory> getRecent6Data(Gwblqhistory pthis) {
        Example example = new Example(Gwblqhistory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("stationid", pthis.getStationid());
        criteria.andEqualTo("devicegroup", pthis.getDevicegroup());
        criteria.andEqualTo("deviceaddr", pthis.getDeviceaddr());
        criteria.andEqualTo("ptaddr", pthis.getPtaddr());
        example.orderBy("datatime").desc();
        RowBounds rb = new RowBounds(0,6);
        List<Gwblqhistory> blqlist = gwblqhistorymapper.selectByExampleAndRowBounds(example, rb);
        return blqlist;
    }
    public MyResult<ptconfig> SelectPt(Gwblqsensor record) {
        MyResult<ptconfig> result = new MyResult<ptconfig>();
        List<ptconfig> list = gwblqsensormapper.selectpt();
        if (list.size() != 0) {
            result.setCode(0);
            result.setMsg("success");
            result.setCount(list!=null&&!list.isEmpty()?list.size():0);
            result.setData(list);
        } else {
            result.setCode(1);
            result.setMsg("数据为空");
        }
        return result;
    }
    @Transactional
    public MyResult<Integer> UpdatePt(ptconfig ptconfig, Integer stationid, Integer devicegroup) {
        Example example = new Example(Gwblqsensor.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("stationid", stationid);
        criteria.andEqualTo("devicegroup", devicegroup);
        gwblqsensormapper.setPtaddr(stationid,devicegroup,ptconfig.getPtaddr());
        if (ptconfig.getBlqmin()!= 0 && ptconfig.getBlqmax()!= 0) {
            Gwblqsensor record = new Gwblqsensor();
            record.setPtaddr(ptconfig.getPtaddr());
            criteria.andGreaterThanOrEqualTo("deviceaddr", ptconfig.getBlqmin());
            criteria.andLessThanOrEqualTo("deviceaddr", ptconfig.getBlqmax());
            updateByExampleSelective(record, example);
        }
        return new MyResult<Integer>(true);
    }

    public List<Gwblqsensor> getPtModel(Gwblqsensor alarm) {
        Example example = new Example(Gwblqsensor.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("stationid", alarm.getStationid());
        criteria.andEqualTo("devicegroup", alarm.getDevicegroup());
        criteria.andEqualTo("ptaddr", alarm.getPtaddr());
        return gwblqsensormapper.selectByExample(example);
    }

    // public List<Gwblqsensor> getWsdModel(Gwblqsensor alarm) {
    //     Example example = new Example(Gwblqsensor.class);
    //     Example.Criteria criteria = example.createCriteria();
    //     criteria.andEqualTo("stationid", alarm.getStationid());
    //     criteria.andEqualTo("devicegroup", alarm.getDevicegroup());
    //     criteria.andEqualTo("wsdaddr", alarm.getWsdaddr());
    //     return gwblqsensormapper.selectByExample(example);
    // }
}
